using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
namespace MicroMsg.UI.Page
{
	public class BackgroundMasterPage : BasePage
	{
		private const string TAG = "BackgroundMasterPage";
		private ObservableCollection<MasterImgGroup> dataSrc = new ObservableCollection<MasterImgGroup>();
		public MasterAlbum masterAlbum;
		private MasterAlbumList list;
		private bool bFirstLoad = true;
		private EventWatcher mWatchSnsInfo;
		internal Grid LayoutRoot;
		internal ListBox MasterGroupList;
		internal Grid StateGrid;
		internal TextBlock StateTxt;
		internal PerformanceProgressBar NetStatusProgressBar;
		private bool _contentLoaded;
		public BackgroundMasterPage()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.BackgroundMasterPage_Loaded));
		}
		private void BackgroundMasterPage_Loaded(object sender, RoutedEventArgs e)
		{
			this.RefreshCover();
			if (this.bFirstLoad)
			{
				if (this.masterAlbum != null && this.masterAlbum.imgGroupList.get_Count() > 1)
				{
					base.get_Dispatcher().BeginInvoke(delegate
					{
						for (int i = 1; i < this.masterAlbum.imgGroupList.get_Count(); i++)
						{
							this.dataSrc.Add(this.masterAlbum.imgGroupList.get_Item(i));
						}
					});
				}
				this.bFirstLoad = false;
			}
		}
		private void RefreshCover()
		{
			if (this.masterAlbum != null)
			{
				MasterPageTitleCtrl.RefreshCover(this.masterAlbum.authorDescription, this.masterAlbum.authorQuote);
			}
		}
		protected override void OnPageIn(object[] object_0)
		{
			Log.smethod_5("BackgroundMasterPage", "OnPageIn");
			this.registerEvent();
			MasterImgGroup masterImgGroup = new MasterImgGroup();
			masterImgGroup.groupName = "AuthorTitle";
			this.dataSrc.Add(masterImgGroup);
			this.MasterGroupList.set_ItemsSource(this.dataSrc);
			this.list = AlbumBackgroundMgr.getMasterAlbum();
			if (this.list != null)
			{
				this.StateGrid.set_Visibility(1);
				this.masterAlbum = this.list.masterAlbumList.get_Item(0);
			}
			base.get_Dispatcher().BeginInvoke(delegate
			{
				if (this.masterAlbum != null)
				{
					for (int i = 0; i < Math.Min(1, this.masterAlbum.imgGroupList.get_Count()); i++)
					{
						this.dataSrc.Add(this.masterAlbum.imgGroupList.get_Item(i));
					}
				}
			});
		}
		private void registerEvent()
		{
			if (this.mWatchSnsInfo == null)
			{
				this.mWatchSnsInfo = new EventWatcher(this, this, new EventHandlerDelegate(this.OnGetPacketEvent));
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_GETPACKAGE_SUCCESS, this.mWatchSnsInfo);
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_GETPACKAGE_ERR, this.mWatchSnsInfo);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchSnsInfo != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_GETPACKAGE_SUCCESS, this.mWatchSnsInfo);
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_GETPACKAGE_ERR, this.mWatchSnsInfo);
				this.mWatchSnsInfo = null;
			}
		}
		private void OnGetPacketEvent(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_GETPACKAGE_ERR)
			{
				this.NetStatusProgressBar.set_Visibility(1);
				this.StateTxt.set_Text(strings.Album_Background_SystemGetFail);
				return;
			}
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_GETPACKAGE_SUCCESS)
			{
				MasterAlbumList masterAlbumList = evtArgs.mObject as MasterAlbumList;
				if (masterAlbumList != null && masterAlbumList.masterAlbumList.get_Count() != 0)
				{
					this.StateGrid.set_Visibility(1);
					this.masterAlbum = masterAlbumList.masterAlbumList.get_Item(0);
				}
				else
				{
					this.NetStatusProgressBar.set_Visibility(1);
					this.StateTxt.set_Text(strings.Album_Background_SystemGetFail);
				}
				if (this.masterAlbum != null)
				{
					using (List<MasterImgGroup>.Enumerator enumerator = this.masterAlbum.imgGroupList.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							MasterImgGroup current = enumerator.get_Current();
							this.dataSrc.Add(current);
						}
					}
					this.MasterGroupList.set_ItemsSource(this.dataSrc);
					MasterPageTitleCtrl.RefreshCover(this.masterAlbum.authorDescription, this.masterAlbum.authorQuote);
				}
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Common/UI/AlbumCover/BackgroundMasterPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.MasterGroupList = (ListBox)base.FindName("MasterGroupList");
			this.StateGrid = (Grid)base.FindName("StateGrid");
			this.StateTxt = (TextBlock)base.FindName("StateTxt");
			this.NetStatusProgressBar = (PerformanceProgressBar)base.FindName("NetStatusProgressBar");
		}
	}
}
